/*
 This file is part of BTStracker
		
    BTStracker is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BTStracker is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.	
	author: azbest.pro (azbest.pro@gmail.com)
*/
package pl.vdl.azbest.mremote.cmd;

import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.logging.Logger;

import pl.vdl.azbest.log.LoggingTheGathering;
import pl.vdl.azbest.mremote.com.CConnect;

public class Invoker {
	private Command command;

	private static Invoker instance;

	
	private LinkedList<Command> llist = new LinkedList<Command>();

	private Invoker() {
	}

	public static Invoker getInstance() {
		if (instance == null) {
			instance = new Invoker();
			return instance;
		} else {
			return instance;
		}
	}

	public void addCommand(Command comm) {
		this.command = comm;
	}

	public void execute() {
		this.command.execute();
	}

	public void addRunnableCommand(Command c) {
		logger.info("Added threaded command");
		llist.add(c);
		if (mrtomato == null) {
			logger.info("Creating new MrTomato");
			MrTomato mrt = new MrTomato();
			mrtomato = new Thread(mrt);
			mrtomato.start();
		} else {
			// do nothing
			logger.info("I ahve another one for MR tomato oooooooooooo :DD");
		}

	}

	private Thread mrtomato = null;

	private class MrTomato implements Runnable {
		public void run() {
			Thread.currentThread().setName("MrTomato");
			logger.info("MrTomato starts.");
			Command c = llist.pollFirst();
			
			while (c != null) {
				logger.info("Have command to execute");
				sendCommandToComm(c);
				c = llist.pollFirst();
				if(c!=null) logger.info("Ok we are done with this one but meantime smb add anothr one.");
				else logger.info("MR tomato has nothing to do ... ");

			}// while
			// cleaning
			mrtomato = null;
			logger.info("MR tomato is DEAD....................................");
		}// run()

		private void sendCommandToComm(Command c) {
			if (c instanceof Cmd_sendCommCommand) {
				Cmd_sendCommCommand cscc = (Cmd_sendCommCommand) c;
				Thread t = new Thread(cscc);
				t.start();
				synchronized (t) {
					try {
						logger.info("Waiting for command to have chance to execute and end its job ...");
						
						t.wait();
						logger.info("... ok mR tomato can continue ...");
					} catch (InterruptedException e) {
						e.printStackTrace();
					}// catch
				}// synchronized

			}// if instanceof
		}
	}// MrTomato

	public void executeThreaded() {
		llist.getFirst();
	}

	private Logger logger = Logger.getLogger(getClass().getName());

	private CConnect conn;
	{
		LoggingTheGathering.addPath(getClass().getName());
	}
}
